import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import { routerMiddleware, push } from 'connected-react-router';
import createLogger from "redux-logger";

import { ADD_LOG_LINES } from "../../../common/redux/logs/actions";

import { SET_SCROLL_POSITION } from "../../../common/redux/appshell/actions";
import { CACHE_REQUEST } from "../../../integrations/ethereum/common/redux/request-cache/actions";
import { RPC_REQUEST_SUCCEEDED, RPC_REQUEST_STARTED } from "../../../integrations/ethereum/common/redux/web3/helpers/ReduxWeb3Provider";

const actionCreators = {
  push,
};

const actionsToIgnoreInConsoleLogger = [
  "APP/BLOCKCHAINSTATE",
  "app/markRequestPending",
  "app/markRequestSuccess",
  "APP/REPLSTATE",
  "APP/REPLSTATE",
  RPC_REQUEST_STARTED,
  RPC_REQUEST_SUCCEEDED,
  ADD_LOG_LINES,
  SET_SCROLL_POSITION,
  CACHE_REQUEST,
];

const logger = createLogger({
  level: "info",
  collapsed: true,
  predicate: (getState, action) => {
    return actionsToIgnoreInConsoleLogger.indexOf(action.type) === -1;
  },
});

export default function configureStore(reducers, history, initialState) {
  const router = routerMiddleware(history);
  
  // If Redux DevTools Extension is installed use it, otherwise use Redux compose
  const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
        // Options: http://zalmoxisus.github.io/redux-devtools-extension/API/Arguments.html
        actionCreators,
        // maxAge: 5, // Default is 50
      }, )
    : compose;

  const enhancer = composeEnhancers(applyMiddleware(thunk, router, logger));

  const store = createStore(reducers, initialState, enhancer);
  return store;
}
